<template>
  <!-- 音频列表 -->
  <div class="chapter">
    <tableComponents :isHavePagination="false" :isShow="false" :isHaveExport="false" @toAdd="editAudioList(true)">
      <template v-slot:table>
        <el-table ref="multipleTable" :data="list" v-loading="loading" stripe header-row-class-name="head_color" tooltip-effect="dark"
            style="width: 99.9%" @selection-change="handleSelectionChange">
          <el-table-column type="selection" class="tc" width="35"></el-table-column>

          <el-table-column label="排序" min-width="80">
            <template slot-scope="scope">{{ scope.row.sort }}</template>
          </el-table-column>

          <el-table-column label="标题" min-width="80">
            <template slot-scope="scope">{{ scope.row.title }}</template>
          </el-table-column>

          <el-table-column label="音频地址" min-width="80">
            <template slot-scope="scope">{{ scope.row.content }}</template>
          </el-table-column>

          <el-table-column prop="name" label="是否免费" align="center" min-width="80">
            <template slot-scope="scope">
              <el-switch class="zdySwith" :width="82" v-model="scope.row.is_free" active-color="#1aad19" inactive-color="#ffffff" active-text="收费"
                  inactive-text="免费" active-value="1" inactive-value="0" @change="getFreeShow($event,scope.row)"></el-switch>
            </template>
          </el-table-column>

          <el-table-column label="操作" align="center" min-width="120">
            <template slot-scope="scope">
              <el-button :loading="$store.state.isLoading" size="mini" type="primary" @click="editAudioList(false,scope.row)">编辑</el-button>
              <el-button :loading="$store.state.isLoading" size="mini" @click="deleteAudioList(scope.$index, scope.row.id)" type="danger">删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </template>
      <template v-slot:footerLeft>
        <a class="aBtn aBtn_1 aBtn_lan2" @click="deleteLises">删除</a>
      </template>
    </tableComponents>

    <el-dialog :close-on-click-modal="false" class="tl size_13" width="800px" @close="class_show = false" title="编辑章节" :append-to-body="true"
        :visible.sync="audio_show">

      <el-form label-width="120px" :model="audioform" :rules="rules" ref="formLabel">

        <el-form-item label="标题：" prop="title">
          <el-col :span="12">
            <el-input type="text" :rows="6" clearable placeholder="请输入" v-model="audioform.title" autocomplete="off"></el-input>
          </el-col>
        </el-form-item>

        <el-form-item label="音频：" prop="video">
          <el-col :span="12">
            <el-input type="text" :rows="6" disabled clearable placeholder="请输入" v-model="audioform.video" autocomplete="off"></el-input>
          </el-col>
          <el-col :span="4" :offset="1">
            <el-button :loading="$store.state.isLoading" type="primary" @click="video_show = true">上传</el-button>
          </el-col>
        </el-form-item>

        <el-form-item label="是否付费：" prop="is_free">
          <el-col :span="12">
            <el-switch v-model="audioform.is_free" active-color="#13ce66" inactive-color="#999999" hone_switch :width="70" active-value="1"
                inactive-value="0"></el-switch>
          </el-col>
        </el-form-item>

        <el-form-item>
          <el-button :loading="$store.state.isLoading" @click="audio_show = false">取 消</el-button>
          <el-button :loading="$store.state.isLoading" type="primary" @click="save('formLabel')">保存</el-button>
        </el-form-item>
      </el-form>

      <el-dialog :close-on-click-modal="false" @close="video_show = false" title="上传音频" :append-to-body="true" :visible.sync="video_show">
        <uploadvideo @send_url="rev_send_url" :pro_src="audioform.video" types="mp3" ref="video_plugin"></uploadvideo>
      </el-dialog>
    </el-dialog>
  </div>
</template>

<script>
import uploadvideo from "@/components/currency/uploadvideo";
export default {
  data () {
    return {
      list: [],
      loading: false,
      multipleSelection: [],
      audio_show: false,
      audioform: {
        id: "",
        title: "",
        video: "",
        is_free: "1",
        times: ""
      },
      rules: {
        title: [{
          required: true,
          message: '请输入标题',
          trigger: 'blur'
        }],
        video: [{
          required: true,
          message: '请上传音频地址',
          trigger: ''
        }]
      },
      video_show: false
    }
  },
  components: {
    uploadvideo
  },
  created () {
    this.getAudioList();
  },
  methods: {
    rev_send_url (obj) {
      //上传音频
      this.audioform.video = obj.url;
      this.audioform.times = obj.video_time;
    },
    getAudioList () {
      //获取音频列表
      this.$api.getNovel.getAudioList({
        id: this.$route.query.id
      }).then(res => {
        if (res.data.code == 200) {
          this.list = res.data.result;
        }
      })
    },
    handleSelectionChange (val) {
      //多选删除
      this.multipleSelection = val;
    },
    editAudioList (bool, row) {
      //编辑添加音频
      if (bool) {
        this.audioform = {
          id: "",
          title: "",
          video: "",
          is_free: "1",
          times: ""
        };
      } else {
        this.audioform = row;
      }
      this.audio_show = true;
    },
    save (formName) {
      //保存编辑
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.audioform.id) {
            //编辑
            this.$api.getNovel.editAudioList(this.audioform).then(res => {
              if (res.data.code == 200) {
                this.$message.success("修改成功");
                this.audio_show = false;
                this.getAudioList();
              } else {
                this.$message.error(res.data.result);
                this.audio_show = false;
              }
            })
          } else {
            //添加
            var params = this.audioform;
            params.id = this.$route.query.id;
            this.$api.getNovel.addAudioList(this.audioform).then(res => {
              if (res.data.code == 200) {
                this.$message.success("添加成功");
                this.audio_show = false;
                this.getAudioList();
              } else {
                this.$message.error(res.data.result);
                this.audio_show = false;
              }
            })
          }
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    getFreeShow (index, row) {
      //是否免费
      row.is_free = index;
      this.$api.getNovel.editAudioList(row).then(res => {
        if (res.data.code == 200) {
          this.getAudioList();
          return;
        } else {
          this.$message.error(res.data.result);
          this.getAudioList();
        }
      })
    },
    deleteAudioList (index, id) {
      //单条数据删除
      this.$swal({
        title: "您确定要删除这些信息吗",
        text: "删除后将无法恢复，请谨慎操作！",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "是的，我要删除",
        cancelButtonText: "容我三思",
      }).then((willDelete) => {
        if (willDelete.value) {
          this.$api.getNovel.delAudioList({
            id_str: id
          }).then((res) => {
            if (res.data.code == 200) {
              this.$swal("删除成功！", "您已经删除了这条信息。", "success");
              this.getAudioList();
            } else {
              this.$swal("删除失败！", res.data.result, "error");
              this.getAudioList();
            }
          });
        }
      });
    },
    deleteLises () {
      //批量删除
      var str = "";
      if (this.multipleSelection.length <= 0) {
        this.$message.error("未选择批量删除");
      } else {
        for (var i in this.multipleSelection) {
          str += this.multipleSelection[i].id + "@";
        }
        str = str.slice(0, -1);
        this.$swal({
          title: "您确定要删除这些信息吗",
          text: "删除后将无法恢复，请谨慎操作！",
          type: "warning",
          showCancelButton: true,
          confirmButtonColor: "#DD6B55",
          confirmButtonText: "是的，我要删除",
          cancelButtonText: "容我三思",
        }).then((willDelete) => {
          if (willDelete.value) {
            var data = {
              id_str: str
            };
            this.$api.getNovel.delAudioList(data).then((res) => {
              if (res.data.code == 200) {
                this.$swal("删除成功！", "您已经删除了这些信息。", "success");
                this.getAudioList();
              } else {
                this.$swal("删除失败！", "出错啦！", "error");
              }
            });
          }
        });
      }
    }
  }
}
</script>

<style>
</style>
